from tool.es_clent import connect_es, store_data_bulk
import json
from tool.string_tool import clean_des
from tool.es_clent import create_index


def process_mashups():
    """
    读取../data中的apis.json数据,批量插入es库中
    :return:
    """
    mashup_datas = json.load(open("../data/mashup.json"))
    insert_data = []
    for mashup in mashup_datas:
        clean_mashup = {
            "category": mashup["categories"],
            "description": clean_des(mashup["desc"]) if mashup["desc"] else None,
            "followers_num": len(mashup["followers"]) if "followers" in mashup and mashup["followers"] else 0,
            "mashup_id": mashup["url"][39:],
            "name": mashup["name"],
            "related_api_ids": [api_id[5:] for api_id in mashup["related_apis"]],
            "time": "20"+mashup["time"] if mashup["time"] else "2016.1.1"
        }
        insert_data.append(clean_mashup)
    store_data_bulk(es_object=es, index_name="mashup", doc_type="mashup", data_list=insert_data)


if __name__ == "__main__":
    es = connect_es()
    es.indices.delete(index="mashup")
    create_index(es, "mashup", "../data/es_index/mashup.json")
    process_mashups()
    # print(es.get(index="mashup", doc_type="mashup", id=1))
